function formatMenu(list) {
    if (Array.isArray(list)) {
        const obj = {};
        const newArr = [];
        list.forEach((item) => {
            // parentKey 菜单父级key
            // key 菜单唯一key
            const { key, parentKey } = item;
            if (!obj[key]) {
                obj[key] = item;
            } else {
                item.children = obj[key].children;
                obj[key] = item;
            }
            if (parentKey) {
                if (!obj[parentKey]) obj[parentKey] = { children: [obj[key]] };
                else if (!obj[parentKey].children) obj[parentKey].children = [obj[key]];
                else obj[parentKey].children.push(obj[key]);
            }
            if (!obj[key]["parentKey"]) {
                newArr.push(obj[key]);
            }
        });
        return newArr;
    }
    return [];
}


module.exports = {
    formatMenu,
};